package com.masterclient.app.businessmodule.usermodule.userinfo.contract;

import com.eoner.common.config.Config;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.businessmodule.usermodule.api.UserHttpService;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.businessmodule.usermodule.login.beans.CaptchaBean;
import com.eoner.baselibrary.bean.personal.CheckMobileBean;
import com.eoner.baselibrary.bean.personal.MobileUpdateBean;
import com.eoner.baselibrary.utils.EncryptUtil;
import com.masterclient.app.utils.CaptchaUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;


public class UpdateMobilePresenter extends UpdateMobileContract.Presenter{

    @Override
    public void check_old_mobile(String old_mobile, String captcha) {
        mRxManager.add(toSubscribe(RetrofitUtil.getInstance().check_old_mobile(old_mobile,captcha), new Consumer<CheckMobileBean>() {
            @Override
            public void accept(CheckMobileBean bean) throws Exception {
                if ("0".equals(bean.getCode()) && bean.getData() != null){
                    mView.check_old_mobile_result(bean.getData().getSh_check_state(),"");
                }else {
                    mView.check_old_mobile_result("0",bean.getMsg());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                o.printStackTrace();
                mView.check_old_mobile_result("0",o.getMessage());
            }
        }));
    }

    @Override
    public void mobiel_change(String old_mobile, String new_mobile, String captcha) {
        mRxManager.add(toSubscribe(RetrofitUtil.getInstance().mobiel_change(old_mobile,new_mobile,captcha), new Consumer<MobileUpdateBean>() {
            @Override
            public void accept(MobileUpdateBean bean) throws Exception {
                if ("0".equals(bean.getCode()) && bean.getData() != null){
                    mView.mobiel_change_result(bean.getData().getSh_update_status(),bean.getData().getSh_update_message());
                }else {
                    mView.mobiel_change_result("0",bean.getMsg());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                o.printStackTrace();
                mView.mobiel_change_result("0",o.getMessage());
            }
        }));
    }

    @Override
    public void getCaptcha(String mobile, String type, String ne) {
        String captchaValidate = "";
        if (CaptchaUtil.CAPTCHA_NUMBER_TIMES >= Config.CAPTCHA_MAX_TIMES){
            Map<String,String> map = new HashMap<>();
            map.put("failureNum","3");
            map.put("clientTime",System.currentTimeMillis()/1000+"");
            captchaValidate = EncryptUtil.encryptRSA(new Gson().toJson(map),Config.captchaKey);
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile",mobile);
            jsonObject.put("type",type);
            jsonObject.put("NECaptchaValidate",ne);
            jsonObject.put("captchaValidate",captchaValidate);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CaptchaBean> observable = UserHttpService.getInstance().getCaptcha(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CaptchaBean>() {
            @Override
            public void accept(@NonNull CaptchaBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.getCaptchaSuccess();
                }else {
                    mView.getCaptchaFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}